/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package futco.patrones.syscand.managedbean.gestionar;

import futco.patrones.syscand.entidades.Persona;

import futco.patrones.syscand.facade.EmpresaFacade;
import futco.patrones.syscand.facade.PersonaFacade;
import futco.patrones.syscand.managedbean.framework.facesconverter.JsfUtil;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;

/**
 *
 * @author casewaredes03
 */
//@Named(value = "personasManagedBean")
//@Dependent
@ManagedBean
@ViewScoped
public class PersonasManagedBean implements Serializable {

    @EJB
    private PersonaFacade facade;
    private Persona persona;
    private Persona personaSeleccionada;
    private List<Persona> personas;
    protected static Logger log = Logger.getLogger(PersonasManagedBean.class);

    public void nuevoRegistro(ActionEvent actionEvent) {
        log.info("nuevoRegistro");
        persona = new Persona();

    }

    public void eliminar(ActionEvent actionEvent) {
        log.info("eliminar");
        try {
            getFacade().eliminar(persona);
            personas = getFacade().buscarTodo();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Información Borrada", persona.toString()));
            persona = new Persona();
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, e.getCause().toString(), e.toString()));
            log.error(e.getCause(), e);

        }
    }

    public void guardarPersona(ActionEvent actionEvent) {
        log.info("guardarPersona");
        try {
            log.debug(persona);
            log.debug(persona.getEmpresa());
            getFacade().guardar(persona);
            personas = getFacade().buscarTodo();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Información Guardada", persona.toString()));
            persona = new Persona();


        } catch (Exception e) {
            log.error(e.getCause(), e);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error al intentar Guardar", e.toString()));
        }
    }

    public void seleccionarRegistro() {
        log.info("seleccionarRegistro");
        log.debug(personaSeleccionada);
        persona = personaSeleccionada;
    }

    /** Creates a new instance of PersonasManagedBean */
    public PersonasManagedBean() {
        log.info("PersonasManagedBean");
        persona = new Persona();
        personas = new LinkedList<Persona>();


    }

    @PostConstruct
    public void init() {
        log.info("init");
        personas = getFacade().buscarTodo();


    }

    public Persona getPersona() {
        return persona;
    }

    public void setPersona(Persona persona) {
        this.persona = persona;
    }

    public List<Persona> getPersonas() {
        return personas;
    }

    public void setPersonas(List<Persona> personas) {
        this.personas = personas;
    }

    public Persona getPersonaSeleccionada() {
        return personaSeleccionada;
    }

    public void setPersonaSeleccionada(Persona personaSeleccionada) {
        this.personaSeleccionada = personaSeleccionada;
    }

    /**
     * @return the facade
     */
    public PersonaFacade getFacade() {
        return facade;
    }

    /**
     * @param facade the facade to set
     */
    public void setFacade(PersonaFacade facade) {
        this.facade = facade;
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(getFacade().buscarTodo(), true);
    }

    @FacesConverter(forClass = Persona.class)
    public static class PersonaControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            PersonasManagedBean controller = (PersonasManagedBean) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "personasManagedBean");
            return controller.getFacade().buscar(getKey(value));
        }

        private java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        private String getStringKey(java.lang.Integer value) {
            StringBuilder sb = new StringBuilder();
            sb.append(value);
            return sb.toString();
        }

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Persona) {
                Persona o = (Persona) object;
                return getStringKey(o.getSecuencia());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + EmpresaFacade.class.getName());
            }
        }
    }
}
